//
//  XJComposeToolBar.swift
//  XJWeiBO
//
//  Created by 452608533 on 16/7/26.
//  Copyright © 2016年 452608533. All rights reserved.
//
enum XJComposeToolBarType:Int{
    case Picture = 1001
    case Mention = 1002
    case Trend = 1003
    case Emoticon = 1004
    case Add = 1005
}
import UIKit

class XJComposeToolBar: UIView {
    var closure:((type: XJComposeToolBarType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        backgroundColor = UIColor.purpleColor()
        
        let pictureButton = addChildButton("compose_toolbar_picture",type: .Picture)
        let mentionButton = addChildButton("compose_mentionbutton_background", type: .Mention)
        let trendButton = addChildButton("compose_trendbutton_background",type: .Trend)
        let emoticonButton = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        let addButton = addChildButton("compose_add_background", type: .Add)
        
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
            
        }
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(trendButton)
            make.leading.equalTo(pictureButton.snp_trailing)
            
        }
        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(emoticonButton)
            make.leading.equalTo(mentionButton.snp_trailing)
            
        }
        emoticonButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(addButton)
            make.leading.equalTo(trendButton.snp_trailing)
            
        }
        addButton.snp_makeConstraints { (make) -> Void in
            make.trailing.top.bottom.equalTo(self)
            make.leading.equalTo(emoticonButton.snp_trailing)
        }
        
    }
    
    func didClickButton(button: UIButton){
        
        closure?(type: XJComposeToolBarType(rawValue: button.tag)!)
        
    }
    
    func addChildButton(imgName: String, type: XJComposeToolBarType)->UIButton {
        
        let button = UIButton()
        //rawValue将枚举名 转换成枚举int值
        button.tag = type.rawValue
        
        button.addTarget(self, action: "didClickButton:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
        
    }
    
}
